﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using BikeSystem.Models;

namespace BikeUI
{
    public partial class FormBase : Form
    {

        protected Account currentAccount = null;
        protected ErrorProvider errorProvider1 = new ErrorProvider();
        protected ErrorProvider errorProvider2 = new ErrorProvider();
        

        public FormBase()
        {
            InitializeComponent();

            btnGroup.ButtonAddNew.Click += new EventHandler(btnNew_Click);
            btnGroup.ButtonUpdate.Click += new EventHandler(btnUpdate_Click);
            btnGroup.ButtonDelete.Click += new EventHandler(btnDelete_Click);
            btnGroup.ButtonSave.MouseDown += new MouseEventHandler(btnSave_Click);
            btnGroup.ButtonCancel.Click += new EventHandler(btnCancel_Click);
        }

        public virtual void CurrentAccount(Account account)
        {
                currentAccount = account;
                btnGroup.Enabled = currentAccount.Role.Equals(ConstData.ACCOUNT_ROLE_ADMIN);
        }

        protected UserControlButtonGroup UCButtonGroup
        {
            get { return btnGroup; }
            set { btnGroup = value; }
        }

        protected virtual void errorProviderClear() {

            errorProvider1.Clear();
            errorProvider2.Clear();
        }

        protected virtual void btnNew_Click(object sender, EventArgs e) { }

        protected virtual void btnUpdate_Click(object sender, EventArgs e) { }

        protected virtual void btnSave_Click(object sender, EventArgs e) { }

        protected virtual void btnDelete_Click(object sender, EventArgs e) { }

        protected virtual void btnCancel_Click(object sender, EventArgs e) {
            errorProviderClear();
        }


        /// <summary>
        /// Test the contents of this text box against a regular expression
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void txtPhone_Validated(TextBox txtPhone)
        {

            bool bTest = txtRegExStringIsValid(txtPhone.Text.ToString());
            if (bTest == false)
            {
                this.errorProvider1.SetError(txtPhone, "This field must contain a phone number (222-333-4444)");
                btnGroup.HasError = true;
            }
            else
            {
                this.errorProvider1.SetError(txtPhone, "");
            }
        }
        /// <summary>
        /// Test for a regex expression match in the text box and
        /// return the results - the example uses a regular
        /// expression used to validate a phone number
        /// </summary>
        /// <returns>boolean</returns>
        private bool txtRegExStringIsValid(string textToValidate)
        {
            Regex TheRegExpression;
            string TheTextToValidate;
            string TheRegExTest = @"[2-9]\d{2}-\d{3}-\d{4}";
            TheTextToValidate = textToValidate;
            TheRegExpression = new Regex(TheRegExTest);
            // test text with expression
            if (TheRegExpression.IsMatch(TheTextToValidate))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// Validate that the textbox contains only numbers
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void txtNumericString_Validated(object sender)
        {

            TextBox txtObject = (TextBox)sender;
            bool bTest = txtNumericStringIsValid(txtObject.Text);
            if (bTest == true)
            {
                this.errorProvider1.SetError(txtObject, "This field must contain only numbers");
                btnGroup.HasError = true;
            }
            else
            {
                this.errorProvider1.SetError(txtObject, "");
            }
        }




        /// <summary>
        /// Test for non-numeric values in the text box and
        /// also make sure the textbox is not empty
        /// </summary>
        /// <returns>boolean</returns>
        private bool txtNumericStringIsValid(string numString)
        {
            if (numString == string.Empty)
            {
                return true;
            }
            char[] testArr = numString.ToCharArray();
            bool testBool = false;
            for (int i = 0; i < testArr.Length; i++)
            {
                if (!char.IsNumber(testArr[i]))
                {
                    testBool = true;
                }
            }
            return testBool;
        }

        /// <summary>
        /// Test to validate that this textbox only contains
        /// alphas
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void txtAlphaString_Validated(TextBox txtObject)
        {
            bool bTest = txtAlphaStringIsValid(txtObject);
            if (bTest == true)
            {
                this.errorProvider1.SetError(txtObject, "This field must contain only alphas");
                btnGroup.HasError = true;
            }
            else
            {
                this.errorProvider1.SetError(txtObject, "");
            }
        }

        /// <summary>
        /// Test for non-alpha values in the text box and
        /// also make sure that the textbox is not empty
        /// </summary>
        /// <returns>boolean</returns>
        private bool txtAlphaStringIsValid(TextBox txtObject)
        {
            // first make sure the textbox contains something
            if (txtObject.Text == string.Empty)
            {
                return true;
            }
            // test each character in the textbox
            char[] testArr = txtObject.Text.ToCharArray();
            bool testBool = false;
            for (int i = 0; i < testArr.Length; i++)
            {
                if (!char.IsLetter(testArr[i]))
                {
                    testBool = true;
                }
            }
            return testBool;
        }


        /// <summary>
        /// Validate that the textbox contains a value
        /// that is between 50 and 100
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void txtRangeValidation_Validated(TextBox txtObject, double low, double high)
        {
            bool bTest = txtRangeValidationIsValid(txtObject.Text, low, high);
            if (bTest == false)
            {
                this.errorProvider1.SetError(txtObject, "This field must contain a number between 50 and 100");
                this.errorProvider2.SetError(txtObject, "");
                btnGroup.HasError = true;
            }
            else
            {
                this.errorProvider1.SetError(txtObject, "");
                this.errorProvider2.SetError(txtObject, "The value is between 50 and 100");
            }
        }

        /// <summary>
        /// Test for numeric values between 50 and 100 in the text box and
        /// return the results
        /// </summary>
        /// <returns>boolean</returns>
        protected bool txtRangeValidationIsValid(string txtObj, double low, double high)
        {
            double tmpVal = 0;
            try
            {
                tmpVal = Convert.ToDouble(txtObj);
            }
            catch { }

            bool testBool = false;
            if (0 == high && 0 == low)
            {
                testBool = true;
            }
            else if (0 == high && tmpVal > low)
            {
                testBool = true;
            }
            else if (tmpVal < high && tmpVal > low)
            {
                testBool = true;
            }
            return testBool;
        }





        /// <summary>
        /// Validate that the text box contains only numbers
        /// or letters
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void txtAlphaNumericString_Validated(TextBox txtObject)
        {
            bool bTest = txtAlphaNumericStringIsValid(txtObject);
            if (bTest == true)
            {
                this.errorProvider1.SetError(txtObject, "This field must contain only alphanumerics");
                btnGroup.HasError = true;
            }
            else
            {
                this.errorProvider1.SetError(txtObject, "");
            }
        }

        /// <summary>
        /// Test for non-alpha values in the text box and
        /// return the results
        /// </summary>
        /// <returns>boolean</returns>
        private bool txtAlphaNumericStringIsValid(TextBox txtObject)
        {
            // Make sure the string is not empty first
            if (txtObject.Text == string.Empty)
            {
                return true;
            }
            // check for alphanumeric values
            char[] testArr = txtObject.Text.ToCharArray();
            bool testBool = false;
            for (int i = 0; i < testArr.Length; i++)
            {
                if (!char.IsLetter(testArr[i]) && !char.IsNumber(testArr[i]))
                {
                    testBool = true;
                }
            }
            return testBool;
        }

        /// <summary>
        /// Validate that this text box contains only special characters
        /// (non-alphanumerics)
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void txtSpecialChars_Validated(TextBox txtObject)
        {
            bool bTest = txtSpecialCharsIsValid(txtObject);
            if (bTest == false)
            {
                this.errorProvider1.SetError(txtObject, "This field must contain only special characters");
                btnGroup.HasError = true;
            }
            else
            {
                this.errorProvider1.SetError(txtObject, "");
            }
        }

        /// <summary>
        /// Test for special character values in the textbox
        /// </summary>
        /// <returns>boolean</returns>
        private bool txtSpecialCharsIsValid(TextBox txtObject)
        {
            // Make sure the string is not empty first
            if (txtObject.Text == string.Empty)
            {
                return false;
            }
            char[] testArr = txtObject.Text.ToCharArray();
            bool testBool = false;
            for (int i = 0; i < testArr.Length; i++)
            {
                if (!char.IsSymbol(testArr[i]))
                {
                    testBool = true;
                }
                if (char.IsLetterOrDigit(testArr[i]))
                {
                    testBool = false;
                }
            }
            return testBool;
        }



        /// <summary>
        /// Validate that the text box contains a minimum
        /// number of characters
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void txtMinLengthTest_Validated(TextBox txtObject)
        {
            bool bTest = txtMinLengthTestIsValid(txtObject);
            if (bTest == true)
            {
                this.errorProvider1.SetError(txtObject, "This field must contain at least 3 characters");
                btnGroup.HasError = true;
            }
            else
            {
                this.errorProvider1.SetError(txtObject, "");
            }
        }

        /// <summary>
        /// Test to see that the textbox contains a minimum number
        /// of characters
        /// </summary>
        /// <returns>boolean</returns>
        private bool txtMinLengthTestIsValid(TextBox txtObject)
        {
            char[] testArr = txtObject.Text.ToCharArray();
            bool testBool = false;
            if (testArr.Length < 3)
            {
                testBool = true;
            }
            return testBool;
        }





    }
}
